<?php
namespace app\wechatxcx\controller;


use app\common\model\ChangguanYuyue;
use app\common\model\ChangguanChangdi;
use app\common\model\ChangguanFeiyong;
use app\common\model\ChangguanFenleiChangdi;
use app\common\model\ChangguanJinyong;
use think\Db;
use \app\common\model\Changguan as ChangguanModel;

use zjkal\ChinaHoliday;

class Changguan extends AllBase
{
    protected function initialize()
    {
        parent::initialize();
    }


    /**
     * 场馆-详情
     * @return \think\response\Json
     */
    public function detail()
    {
        $where = array(
            'id'=>input('id',0)
        );
        $data = ChangguanModel::getOne($where);

        $flid = ChangguanChangdi::getColumn(array('cgid'=>$data['id']),'flid');

        $data['cate'] = ChangguanFenleiChangdi::getColumn(array('id'=>array('in',$flid)),'label');


        return returnJson(200, 'success', $data);
    }


    /**
     * 预约详情
     * @return \think\response\Json
     */
    public function yuyuedetail()
    {
        //查询条件
        $where = array(
            'cgid'=>input('cgid'),
            'status'=>ChinaHoliday::isHoliday(input('date'))?2:1
        );

        //所有时段
        $time = ChangguanFeiyong::getAll($where,'start_time asc','start_time,end_time,price');

        $flid = ChangguanFenleiChangdi::getValue(array('label'=>input('cate')),'id');
        $cdid = ChangguanChangdi::getColumn(array('cgid'=>input('cgid'),'flid'=>$flid),'cdid');
        $changdi = ChangguanFenleiChangdi::getColumn(array('id'=>array('in',$cdid)),'id,label,pid');

        if(!$time){
            return returnJson(0, '', $time);
        }

        //长期禁用
        $where['status'] = 1;
        $where['week'] = date('w',strtotime(input('date')));
        $changqi_jinyong = ChangguanJinyong::getAll($where);

        //一次性禁用
        unset($where['week']);
        $where['date'] = input('date');
        $where['status'] = 2;
        $one_jinyong = ChangguanJinyong::getAll($where);

        //预约
        unset($where['status']);
        $where['checked'] = 1;
        $yuyue = ChangguanYuyue::getAll($where);

        $is_today = date('Y-m-d') == input('date');
        $hour = date('H');

        foreach ($changdi as &$cd){
            foreach ($time as &$v){
                $time_hour = substr($v['start_time'],0,2);
                if($is_today && $time_hour<$hour){
                    $v['jinyong'] = true;
                }else{
                    $v['jinyong'] = false;
                    if($one_jinyong){
                        foreach ($one_jinyong as $k=>$vv){
                            if($vv['start_time']==$v['start_time']){
                                $v['jinyong'] = true;
                            }
                        }
                    }
                    if($changqi_jinyong){
                        foreach ($changqi_jinyong as $k=>$vv){
                            if($vv['start_time']==$v['start_time']){
                                $v['jinyong'] = true;
                            }
                        }
                    }
                }

                $v['yuyue'] = false;
                if($yuyue){
                    foreach ($yuyue as $k=>$vv){
                        if($v['start_time']>=$vv['start_time'] && $v['end_time']<=$vv['end_time'] && $vv['cdid']==$cd['id']){
                            $v['yuyue'] = true;
                        }
                    }
                }
            }
            $cd['time'] = $time;
        }

        return returnJson(200, '', compact('time','changdi'));
    }





}